﻿

Namespace Processing
    Public Class DeleteAspNetFolders
        Inherits BaseProcess(Of DeleteAspNetFolders)
        Implements Interfaces.IProcess

        Public Sub Action() Implements Interfaces.IProcess.Action
            CleanASPNetCache()
        End Sub

        Public ReadOnly Property Name As String Implements Interfaces.IProcess.Name
            Get
                Return "Cleaning Asp.net Cache"
            End Get
        End Property

        Private Shared Sub CleanASPNetCache()
            Dim sysvol As String = Environment.SystemDirectory.Substring(0, 1)
            For x As Integer = 0 To 3
                DeleteDirectoryPass("{0}:\Windows\Microsoft.NET\Framework{1}\", sysvol)
            Next
        End Sub

        Private Shared Sub DeleteAspNetTempDirectory(ByVal directory As String, ByVal versionToDelete As String)
            If directory.Contains(versionToDelete) Then
                Dim str As String = My.Computer.FileSystem.CombinePath(directory, "Temporary ASP.NET Files")
                If My.Computer.FileSystem.DirectoryExists(str) Then
                    My.Computer.FileSystem.GetDirectories(str).AsParallel.ForAll(Sub(dir)
                                                                                     Try
                                                                                         My.Computer.FileSystem.DeleteDirectory(dir, FileIO.DeleteDirectoryOption.DeleteAllContents)
                                                                                     Catch ex As Exception
                                                                                         'Do nothing just skip
                                                                                     End Try
                                                                                 End Sub)
                End If
            End If
        End Sub

        Private Shared Sub DeleteDirectoryPass(ByVal rootDir As String, ByVal sysvol As String)
            My.Computer.FileSystem.GetDirectories(String.Format(rootDir, sysvol, "")).AsParallel.ForAll(Sub(dir)
                                                                                                            DeleteAspNetTempDirectory(dir, "v2")
                                                                                                            DeleteAspNetTempDirectory(dir, "v4")
                                                                                                        End Sub)
            If Environment.Is64BitOperatingSystem Then
                My.Computer.FileSystem.GetDirectories(String.Format(rootDir, sysvol, "64")).AsParallel.ForAll(Sub(dir)
                                                                                                                  DeleteAspNetTempDirectory(dir, "v2")
                                                                                                                  DeleteAspNetTempDirectory(dir, "v4")
                                                                                                              End Sub)
            End If
        End Sub


    End Class
End Namespace

